const apiBase = 'http://115.190.86.189:5000/api';
const outputEl = document.getElementById('output');
const buttons = document.querySelectorAll('button');

async function callApi(endpoint, method = 'GET') {
    setLoading(true);
    outputEl.textContent = '请求中...';
    try {
        const res = await fetch(`${apiBase}/${endpoint}`, { method });
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const data = await res.json();
        const statusText = data.success ? '✅ 成功' : '❌ 失败';
        outputEl.textContent = `${statusText}\n${data.output}`;
    } catch (error) {
        outputEl.textContent = `❌ 请求错误: ${error.message}`;
    } finally {
        setLoading(false);
    }
}

function setLoading(isLoading) {
    buttons.forEach(btn => btn.disabled = isLoading);
    outputEl.classList.toggle('loading', isLoading);
}

// 绑定事件监听
document.getElementById('btn-start').onclick = () => callApi('start');
document.getElementById('btn-status').onclick = () => callApi('status');
document.getElementById('btn-restart').onclick = () => callApi('restart');
document.getElementById('btn-update').onclick = () => callApi('update');
document.getElementById('btn-stop').onclick = () => callApi('stop');
document.getElementById('btn-backup').onclick = () => callApi('backup');
